package client;

import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

import communication.ClientComm;
import communication.ServerComm;

public class ClientCommRMI extends UnicastRemoteObject implements ClientComm {
	private static final long serialVersionUID = 1L;
	
	private ServerComm server;
	private ClientModel client;
	
	public ClientCommRMI(ClientModel cl, String host) throws RemoteException {
		client = cl;
		try {
			// get the reference of the servercomm obj
			server = (ServerComm)Naming.lookup(("rmi://127.0.0.1/server:1999"));
			// give the reference of this client to the servercomm obj
			server.connect(this);
			client = cl;
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@Override
	public void msgFromServer(String msg) throws RemoteException {
		client.arrivaMsgFromServer(msg);
	}

	@Override
	public void sendRequest(String msg) throws RemoteException {
		server.msgFromClient(msg);
	}

	@Override
	public void clearMessages() throws RemoteException {
		// TODO Auto-generated method stub

	}

	@Override
	public void close() throws RemoteException {
		// TODO Auto-generated method stub

	}

}
